-- AutoFight
-- Create by luohj
-- 外挂

module("AutoFight", package.seeall);

function autoOpt(uiGrid, point)
    local hasOpt = false;

    -- 复活界面
    local isRevive = false;
    if not hasOpt then
        if UIMgr.getCurrentScene():isOpen("UIDungeonRevive") then
            isRevive = DungeonActionM.go("revive");
            hasOpt = true;
            if not isRevive then
                point.dungeonTopMenu:setNotInFight();
                return false, "Die";
            else
                UIMgr.getCurrentScene():removeFormByName("UIDungeonRevive");
                -- point.dungeonTopMenu:reviveContinue();
            end
            return false;
        end
    end

    local hp = ME.user:getHp();
    if hp <= 0 then
    	hasOpt = true;
    end

    -- 检测是否有选择框出现 并进行选择
    if not hasOpt then
        for _, dialog in pairs(UIMgr.getCurrentScene().dialogs) do
            if dialog:getName() == "UIConfirm" then
                if not dialog.onOkClick(dialog, ccui.TouchEventType.ended) then
                    dialog.onCancelClick(dialog, ccui.TouchEventType.ended)
                end
                hasOpt = true;
                break;
            end
        end
    end

    -- 打开所有能打开的格子
    if not hasOpt then
        for i = 1, TOTAL_GRID_NUM do
            if DungeonM.canOpenGrid(i) == GRID_OPEN_OK then
                if uiGrid[i].openGridClick then
                    uiGrid[i].openGridClick(uiGrid[i], ccui.TouchEventType.began);
                    hasOpt = true;
                end
                break;
            end
        end
    end

    -- 如果神龙许愿打开 则许愿并将其关闭
    if not hasOpt then
        if UIMgr.getCurrentScene():isOpen("UIDragonWish") then
            DungeonActionM.go("do_dragon_wish", 0, 1);
            UIMgr.getCurrentScene():removeFormByName("UIDragonWish");
            hasOpt = true;
        end
    end

    local grids = DungeonM.getCurrentDungeon();

    -- 占领矿脉
    if not hasOpt then
        for i = 1, TOTAL_GRID_NUM do
            local grid = uiGrid[i];
            if grids[i]:isOpened() and grid:isCleared() and grid.gridData.type == GRID_TYPE_MINE and grid.pickMineClick then

                -- 触发访问建筑的事件，只触发一次
                if not grid.visitAltar then
                    DungeonActionM.go("visit_building", grid.gridData:getPos());
                    grid.visitAltar = true;
                end

                if DungeonActionM.go("occupy_mine", i) then
                    uiGrid[i].pickMineClick = nil;
                    hasOpt = true;
                end
                break;
            end
        end
    end

    -- 判断是否还有怪物
    local isHasMonster = false;
    local isHasBoss = false;
    for i = 1, TOTAL_GRID_NUM do
        local grid = uiGrid[i];
        if not grid:isCleared() then
            if grid.gridData:isMonster() then
                isHasMonster = true;
--                break;
            elseif grid.gridData.type == GRID_TYPE_BOSS then
            	isHasMonster = true;
            	isHasBoss = true;
                break;
            end
        end
    end

    -- 格子
    if not hasOpt and isHasBoss == false then
        for i = 1, TOTAL_GRID_NUM do
            local grid = uiGrid[i];
            if grids[i]:isOpened() and grid:isCleared() and (grids[i].bonus or grids[i].has_key) then
                local click = nil;

                -- 先拾取血瓶/蓝瓶/探索点
                if  grid.gridData.type == GRID_TYPE_ITEM and
                    (grids[i].bonus[2] == "hp" or grids[i].bonus[2] == "mp" or grids[i].bonus[2] == "search_point") then

                    click = uiGrid[i].pickItemClick or click;
                end

                if not isHasMonster and grids[i]:isOpened() then

                    -- 物品
                    click = uiGrid[i].pickItemClick or click;

                    -- 宝箱/boss宝箱
                    click = uiGrid[i].pickBoxClick or click;

                    -- 冈布奥
                    click = uiGrid[i].pickSlimeClck or click;

                    -- 学徒
                    click = uiGrid[i].pickApprentice or click;

                end

                -- 地图没用到屏蔽了
                --click = uiGrid[i].pickMapClick or click;

                if click and click(uiGrid[i], ccui.TouchEventType.began) then
                    hasOpt = true;
                    break;
                end
            end
        end
    end

    -- 遗产
    if not hasOpt and isHasBoss == false then
        for i = 1, TOTAL_GRID_NUM do
            local grid = uiGrid[i];
            if grids[i]:isOpened() and grid:isCleared() and grid.gridData.type == GRID_TYPE_LEGACY and grid.pickLegacyClick then
                grid.pickLegacyClick(grid, ccui.TouchEventType.began);
                grid.pickLegacyClick = nil;
                hasOpt = true;
                break;
            end
        end
    end

    -- 商店
    if not hasOpt then
        for i = 1, TOTAL_GRID_NUM do
            local grid = uiGrid[i];
            if grids[i]:isOpened() and grid:isCleared() and grid.gridData.type == GRID_TYPE_SHOP then

                -- 获取商店类型
                local shopType = grid.gridData.shopType;
                local goodsList = DungeonShopM.getSellGoodsByType(shopType .. i);
                local goodsIndex = 1;
                for _, info in pairs(goodsList) do
                    local ret = DungeonShopM.canBuyGoods(i, shopType .. i, goodsIndex);
                    if ret == true then

                        -- 执行购买
                        DungeonActionM.go("buy_goods", i, goodsIndex);
                    	hasOpt = true;
                    	break;
                    end
                    goodsIndex = goodsIndex + 1;
                end
                if hasOpt then
                	break;
                end
            end
        end
    end

--    -- 检测是否有副本
--    if not hasOpt and isHasBoss == false then
--        for i = 1, TOTAL_GRID_NUM do
--            local grid = uiGrid[i];
--            if grids[i]:isOpened() and grid:isCleared() and grid.gridData.type == GRID_TYPE_INSTANCE and grid.instanceClick then
--                grid.instanceClick(grid, ccui.TouchEventType.began);
--                grid.instanceClick = nil;
--                hasOpt = true;
--                break;
--            end
--        end
--    end


    -- 攻击怪物 (法术)
    if not hasOpt then
        local spells = ItemM.getSpellCanUse();

        for _, classId in pairs(spells) do
            local skillId = SpellM.query(classId, "cast_skill_id");
            local targetType = SkillM.query(skillId, "target");
            if targetType == ATTACKER then

                -- 被动技能，不需要选择目标
                hasOpt = true;
                DungeonActionM.go("use_spell", -1, classId);
                EventMgr.fire(event.PLAYER_MOVE, -1);
                break;

            elseif targetType == DEFENSER then
            -- 对敌方施放
            -- 主动技能，选择目标
                for i = 1, TOTAL_GRID_NUM do
                    local grid = uiGrid[i];
                    if grids[i]:isOpened() and not grid:isCleared() and (grid.gridData:isMonster()) then
                        DungeonActionM.go("use_spell", i, classId);
                        EventMgr.fire(event.PLAYER_MOVE, pos);
                        hasOpt = true;
                        break;
                    end
                end
            end
            if hasOpt then
            	break;
            end
        end

    end

    -- 攻击怪物 (物理)
    if not hasOpt then
        for i = 1, TOTAL_GRID_NUM do
            local grid = uiGrid[i];
            if grids[i]:isOpened() and not grid:isCleared() and (grid.gridData:isMonster()) then

                uiGrid[i].attackMonsterClick(uiGrid[i], ccui.TouchEventType.began);
                if uiGrid[i].attackMonsterClick(uiGrid[i], ccui.TouchEventType.ended) then
                    hasOpt = true;
                    break;
                end
            end
        end
    end

    -- 关闭所有对话框
    if not hasOpt then
        for _, dialog in pairs(UIMgr.getCurrentScene().dialogs) do
            if dialog:getName() == "UIHint" and dialog.autoClose == false then
--            if dialog:getName() == "UIHint" then
                UIMgr.getCurrentScene():removeDialog(dialog);
                hasOpt = true;
            end
        end
    end

     -- 如果在副本中可随时退出
    if not hasOpt then
        if DungeonInstanceM.isInInstance() then
            local grid = DungeonInstanceM.getTargetGrid();
            DungeonActionM.go("take_instance_bonus", grid:getPos());
            DungeonActionM.go("leave_instance");
            hasOpt = true;
        end
     end

    -- 进入下一关
    if not hasOpt then
        local enterNext = false;
        for i = 1, TOTAL_GRID_NUM do
            if not DungeonServiceM.getAutoOpeningFlag() and uiGrid[i].canEnterNextLevel then
                EventMgr.fire(event.TRY_ENTER_NEXT_FLOOR);
                enterNext = true;
                return false;
            end
        end
    end

    return true;
end
